// pages/msg-list/msg-list.js
const app = getApp();
var page = 1;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    isEmty: true,
    newsList: [],
  },


  openMsgDetail: function(e) { //资讯详情
    wx.navigateTo({
      url: '../msg-detail/msg-detail?id='+e.currentTarget.dataset.id,
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.isEmty = true;
    this.setData({
      is_special:options.is_special || 0
    })
    page = 1;
    this.data.newsList = [];
    this.getHotArticle();
  },

  getHotArticle: function() {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: app.globalData.host + 'article/index',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      data: {
        page: page,
        is_special: that.data.is_special
      },
      success(res) {
        wx.hideLoading();
        console.log(res)
        var isEmty = that.data.isEmty;
        var { current_page, last_page, data } = res.data.data.list;
        console.log(data)
        //data为获取的列表数据

        if (isEmty) {
          that.data.newsList = data;
          that.data.isEmty = false;
          page = 2;
        } else {
          if (data.length > 0) {
            console.log(22222222222)
            console.log(that.data.newsList)
            var newsList = that.data.newsList;
            newsList = newsList.concat(data);
            that.setData({
              newsList
            })
            console.log(that.data.newsList)
            page++;
          } else {
            console.log(333333333)

            var newsList = that.data.newsList;
            newsList = newsList.concat(data);
            that.setData({
              newsList
            })
            wx.showToast({
              title: '没有更多资讯了',
              icon: 'none',
              duration: 1000,
            })
          }
        }
        that.setData({
          newsList: that.data.newsList
        })

      }

    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    console.log("下拉刷新")
    wx.showLoading({
      title: '加载中',
    })
    this.data.isEmty = true;
    page = 1;
    this.data.newsList = [];
    this.getHotArticle();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (page > 1) {
      this.getHotArticle();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})